var action = 'http://localhost:20001';
var init = 0;
var http = null, form = null, layedit = null,
    table = null, laydate = null, element = null, dtree = null, upload = null, formSelects = null,
    soda = null, app = null, util = null, tree = null, common_upload = null, selectM = null;
// 公共缓存key
var cache = {
    platform: 'lazy-code',
    system: 'lazy-code-system',
    token: 'lazy-code-token',
    user: 'lazy-code-user',
    menu: 'lazy-code-menu',
};
// 路由
var api = {
    base: {
        user: '/v1/base/user',
        dept: '/v1/base/dept',
        dept_user: '/v1/base/dept-user',
        label: '/v1/base/label'
    },
    common: {
        auth: '/v1/common/auth',
        generate: '/v1/common/generate',
        exit: '/v1/common/exit',
    },
    soda: {
        grown: '/v1/baby/grown',
        server: '/base/v1/config/server'
    },
    tps: {
        oss: '/v1/tps/oss'
    },
    system: {
        module: '/v1/system/module',
        auth: '/v1/system/authorities',
        user: '/v1/sys/user',
        role: '/v1/system/role',
        log: {
            data: '/v1/log/data',
            ex: '/v1/log/ex',
            login: '/v1/log/login',
        },
        dict: '/v1/system/dict',
    },
    crm: {
        base: '/crm/v1/data/crm/client'
    },
    data: {
        model: '/v1/data/model'
    },
    global: {
        user: '/base/v1/common/user'
    },
    cms: {
        project: '/cms/v1/project',
        case: '/cms/v1/case',
        adviser: '/cms/v1/adviser'
    }
}

// 初始化
function initialize(_config) {
    // 默认添加组件
    var module = ['form', 'kit', 'jquery', 'element', 'app', 'util', 'table', 'laydate'];
    // 动态加载组件
    if (_config.module) {
        for (let m in _config.module) {
            module.push(_config.module[m]);
        }
    }
    // 独立组件
    if (init == 0) {
        layui.extend({
            formSelects: 'formSelects/formSelects-v4.min',
            dtree: 'ext/dtree/dtree'
        })
        init += 1;
    }

    // 初始化
    layui.config({
        base: '/module/'
    }).use(module, function () {
        module_init();
        try{
            _config.init ? _config.init() : '';
        }catch (e) {
            setTimeout(function () {
                _config.init ? _config.init() : '';
            }, 200);
        }
    });
    if (_config.title) {
        $('#body-content-header').vm({title: _config.title});
    }
    if (_config.auth) {
        $('#body-content-button').vm({btns: _config.auth})
    }

}

// 初始赋值
function module_init() {
    soda = layui.kit;
    element = layui.element;
    app = layui.app;
    util = layui.util;
    table = layui.table;
    layedit = layui.layedit;
    form = layui.form;
    laydate = layui.laydate;
    tree = layui.tree;
    dtree = layui.dtree;
    common_upload = layui.common_upload;
    upload = layui.upload;
    formSelects = layui.formSelects;
}

// 读取页面菜单权限
function get_permission(_type) {
    // 获取当前路由
    let url = window.location.href;
    let item = url.split('!');
    let route = item[1];
    let menus = soda.session.get(cache.menu);
    let permission = [];
    let tem = [];
    $.each(menus, function (i, item) {
        if (item.url == route) {
            permission = item.permission;
            if (permission.length > 0) {
                $.each(permission, function (i, p) {
                    if (p.type == _type) {
                        tem.push(p);
                    }
                })
            }
        }
    })
    return tem;
}

// 处理权限
var page_auth = {
    top: function () {
        let permission = get_permission(1);
        let html = '';
        if (permission.length > 0) {
            $.each(permission, function (i, p) {
                html += '<button class="layui-btn layui-btn-sm" lay-event="' + p.event + '">' + p.name + '</button>';
            })
        }
        return '<div class="layui-btn-container">' + html + '</div>';
    },
    right: function () {
        let permission = get_permission(2);
        let html = '';
        if (permission.length > 0) {
            $.each(permission, function (i, p) {
                let color = p.event.indexOf('delete') >= 0 ? 'layui-btn-danger' : p.color;
                html += '<a class="layui-btn ' + color + ' layui-btn-xs" lay-event="' + p.event + '">' + p.name + '</a>'
            })
            html = '<div>' + html + '</div>'
        }
        return html;
    }
}

